#include "cards.h"

cards_ptr *get_cards(void) {
	json_t *cards_file;
	json_error_t error;
	cards_ptr * first;
	cards_ptr * last;
	cards_file = json_load_file("../cards.json", 0, &error);

	if(cards_file == NULL) {
		printf("Cards Not Loaded!\n");
		exit(EXIT_FAILURE);
	}


	//loop to load the cards data from the file | in the moment only work with monster cards
	for(unsigned long i = 0; i < json_array_size(cards_file); i++) {
		//pega cada dado das cartas e coloco num struct; May i do a LL?
		json_t * card = json_array_get(cards_file, i);
		cards_ptr *tmp = malloc(sizeof(cards_ptr));

		//initializing the pointers
		if( i == 0 ) {
			first = tmp;
			last = tmp;
		}
		else last->next = tmp;

		//getting the data OBS: I think doenst work cause return is json_t
		tmp->card_type = (char *)json_string_value(json_object_get(card, "card_type"));
		if(strcmp(tmp->card_type, "Monster_Card") == 0) {
			tmp->card = malloc(sizeof(struct Monster_Card));
			((struct Monster_Card*)tmp->card)->name = (char *)json_string_value(json_object_get(card, "name"));
			((struct Monster_Card*)tmp->card)->attribute = (char *)json_string_value(json_object_get(card, "attribute"));
			((struct Monster_Card*)tmp->card)->type = (char *)json_string_value(json_object_get(card, "monster_type"));
			((struct Monster_Card*)tmp->card)->level = json_number_value(json_object_get(card, "level"));
			((struct Monster_Card*)tmp->card)->atk = json_number_value(json_object_get(card, "atk"));
			((struct Monster_Card*)tmp->card)->def = json_number_value(json_object_get(card, "def"));
		}
		//now that is completed tmp is the last one
		last = tmp;
	}
	return first;
}
